#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QGridLayout>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建9个按钮，使用网格布局按照3*3的方式排列
    QPushButton* button1 = new QPushButton("按钮1");
    QPushButton* button2 = new QPushButton("按钮2");
    QPushButton* button3 = new QPushButton("按钮3");
    QPushButton* button4 = new QPushButton("按钮4");
    QPushButton* button5 = new QPushButton("按钮5");
    QPushButton* button6 = new QPushButton("按钮6");
    QPushButton* button7 = new QPushButton("按钮7");
    QPushButton* button8 = new QPushButton("按钮8");
    QPushButton* button9 = new QPushButton("按钮9");

    // 设置按钮的尺寸策略，水平方向和垂直方向都是拉伸状态
    button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button7->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button8->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button9->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 创建网络布局管理器，把按钮添加进去
    QGridLayout* layout = new QGridLayout();
    this->setLayout(layout);
    layout->addWidget(button1, 0, 0);
    layout->addWidget(button2, 0, 1);
    layout->addWidget(button3, 0, 2);
    layout->addWidget(button4, 1, 0);
    layout->addWidget(button5, 1, 1);
    layout->addWidget(button6, 1, 2);
    layout->addWidget(button7, 2, 0);
    layout->addWidget(button8, 2, 1);
    layout->addWidget(button9, 2, 2);

    // 设置水平拉伸系数，三个列按照1:1:2的比例排列
    layout->setColumnStretch(0, 1);
    layout->setColumnStretch(1, 1);
    layout->setColumnStretch(2, 2);

    // 设置垂直拉伸系数
    // 如果某一行的拉伸系数设为0，意思是不参与拉伸，此行按钮的高度是固定值（列也一样）
    // 第1行不参与拉伸，第0行和第2行的高度比为1:3
    layout->setRowStretch(0, 1);
    layout->setRowStretch(1, 0);
    layout->setRowStretch(2, 3);
}

Widget::~Widget()
{
    delete ui;
}

